package {{.PkgName}}

import (
    "errors"
	"net/http"

    "gitee.com/libwiki/go-zero-lib/package/response"

	{{if .HasRequest}}"github.com/zeromicro/go-zero/rest/httpx"{{end}}
	{{.ImportPackages}}
)

func {{.HandlerName}}(svcCtx *svc.ServiceContext) http.HandlerFunc {
	return func(w http.ResponseWriter, r *http.Request) {
	    // 错误拦截
	    defer func() {
            if result := recover(); result != nil {
                errMessage, ok := result.(string)
                //errMessage := fmt.Sprintf("%v", result)
                if ok { // 如果是字符串则直接返回结果
                    response.Response(w, nil, errors.New(errMessage))
                } else {
                    panic(result)
                }

            }
    	}()
		{{if .HasRequest}}var req types.{{.RequestType}}
            if err := httpx.Parse(r, &req); err != nil {
                httpx.Error(w, err)
                return
            }
		{{end}}
		l := {{.LogicName}}.New{{.LogicType}}(r.Context(), svcCtx)

		{{if .HasResp}}resp, {{end}}err := l.{{.Call}}({{if .HasRequest}}&req{{end}})

		{{if .HasResp}}
		    response.Response(w, resp, err)
		{{else}}
		    response.Response(w, nil, err)
		{{end}}
	}
}
